;; supercollider/server-options.scm - (c) rohan drape, 2003-2005

;; A table of the default server options.  Each record has three
;; elements, the option letter for scsynth, the long name for the
;; option, and the default value.

(define (server-options-default)
  '((u udp-port-number 57110)
    (t tcp-port-number 57110)
    (c number-of-control-bus-channels 4096)
    (a number-of-audio-bus-channels 128)
    (i number-of-input-bus-channels 2)
    (o number-of-output-bus-channels 2)
    (z block-size 64)
    (Z hardware-buffer-size 0)
    (S hardware-sample-rate 0)
    (b number-of-sample-buffers 1024)
    (n maximum-number-of-nodes 1024)
    (d maximum-number-of-synth-defs 1024)
    (m real-time-memory-size 2048)
    (w number-of-wire-buffers 64)
    (r number-of-random-seeds 64)
    (D load-synthdefs? 1)
    (l max-logins 64)
    (p session-password "go")))

;; Make a new value with `option', which may be either the option
;; letter or the long name, set to value.

(define (server-options/revision opt key value)
  (map (lambda (o)
	 (match o ((list letter name current)
		   (if (or (eq? key letter) 
			   (eq? key name))
		       (list letter name value)
		       o))))
       opt))

;; Read the value `option', which may be either the option letter or
;; the long name, at `server-options'.

(define (server-options-get opt key)
  (let loop ((records opt))
    (if (null? records)
	(error "server-options-get: no such option" key)
	(let ((next (first records)))
	  (if (or (eq? key (first next)) (eq? key (second next)))
	      (third next)
	      (loop (cdr records)))))))

;; Construct the string setting all scsynth options to those at `o'.
;; This string is used when booting a server.

(define (server-options->options-string opt)
  (apply 
   string-append 
   (map (lambda (record)
	  (format " -~a ~a" (car record) (caddr record)))
	opt)))

;; current-server

(define (server-options) 
  (server_-options (current-server)))

(define (server-options-string)
  (server-options->options-string (server-options)))

(define (server-option key . value)
  (let ((opt (server-options)))
    (if (null? value)
	(server-options-get opt key)
	(server-options/revision opt key (car value)))))
